/*
 * Copyright (C) 2014 Rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.camera;

import rpg.RPG;
import rpg.entities.Entity;
import rpg.maps.Map;
import rpg.util.Vector;

/**
 *
 * @author Rothens
 */
public class CMGoto extends CameraMovement {

    int x = 0;
    int y = 0;
    int xmin = RPG.WINDOW_WIDTH / 2;
    int ymin = RPG.WINDOW_HEIGHT / 2;
    int xmax = 0;
    int ymax = 0;
    float speed = 0;
    Entity e = null;
    Vector speedVector;

    /**
     *
     * @param x target X coordinate.
     * @param y target Y coordinate.
     * @param speed tile/sec
     * @param camera
     */
    public CMGoto(int x, int y, float speed, Camera camera) {
        super(camera);
        this.x = x;
        this.y = y;
        this.speed = speed;
        init();
    }

    public CMGoto(Entity e, float speed, Camera camera) {
        super(camera);
        this.speed = speed;
        this.e = e;
        init();
        System.out.printf("Entity %d, %d\n", e.getCurrentLocation().getX(), e.getCurrentLocation().getY());

    }

    public void init() {
        Map act = camera.getActualMap();
        xmax = act.getWidth() * 64 - xmin;
        ymax = act.getHeight() * 64 - ymin;
        if (e != null) {
            x = e.getCurrentLocation().getX();
            y = e.getCurrentLocation().getY();
        }
        x = Math.min(xmax, Math.max(x, xmin));
        y = Math.min(ymax, Math.max(y, ymin));
        calcVector();
    }

    public void calcVector() {
        speedVector = new Vector(x - camera.getX(), y - camera.getY()).normalize().mul(64d * speed);
    }

    @Override
    public boolean tick() {
        if (e != null) {
            int tmpx;
            int tmpy;
            tmpx = e.getCurrentLocation().getX();
            tmpy = e.getCurrentLocation().getY();
            tmpx = Math.min(xmax, Math.max(tmpx, xmin));
            tmpy = Math.min(ymax, Math.max(tmpy, ymin));
            //if (x != tmpx || y != tmpy) {
                x = tmpx;
                y = tmpy;
                calcVector();
            //}
        }


        double dist = new Vector(x, y).distanceSq(new Vector(camera.getX(), camera.getY()));
        if (dist < speedVector.lengthSq()) {
            camera.setX(x);
            camera.setY(y);
            return true;
        }
        int cx = (int) (camera.getX() + speedVector.getX());
        int cy = (int) (camera.getY() + speedVector.getY());
        camera.setX(cx);
        camera.setY(cy);
        return false;
    }

    @Override
    public String toString() {
        return String.format("CMGoto %d,%d, %f", x, y, speed);
    }
}
